package leetcode.code1954;

import java.util.ArrayList;
import java.util.List;

import leetcode.IDebug;
import leetcode.helper.H;

public class Solution implements IDebug {

	// 组合打表，有必要就升级long
	static long[] sums4;
	static int size = 63000;

	static {
		long[] sums = new long[size];
		sums4 = new long[size];
		sums[0] = 0;
		for (int i = 1; i < size; i++) {
			sums[i] = sums[i - 1] + i;
			sums4[i] = (((i << 1) + 1) * sums[i]) * 4;
		}
//		System.out.println(sums[size - 1] <= (int) 1e15);
	}

	public long minimumPerimeter(long neededApples) {
		int l = 0, r = size - 1;
		int ans = 0;
		while (l <= r) {
			int mid = ((r - l) >> 1) + l;
			if (sums4[mid] >= neededApples) {
				ans = mid;
				r = mid - 1;
			} else {
				l = mid + 1;
			}
		}
		return ans * 8;
	}

	@Override
	public void debug4() {
		H.compare(8, this.minimumPerimeter(1));

	}

	@Override
	public void debug3() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug2() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug1() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		Solution so = new Solution();
		so.debug1();
		so.debug2();
		so.debug3();
		so.debug4();

	}

}
